<?php
/**
 * Created by PhpStorm.
 * User: lenovo
 * Date: 2017/11/17
 * Time: 17:44
 */

namespace Admin\Controller;


class TestController extends CommonController
{
    /**
     * 管理员列表显示
     */
    public function test_list()
    {
        //安监局角色查询该公司所有风险点和危险源
        $admin_id =$_SESSION['admin_id'];

            //查询当前用户负责的风险点，
            //  $sql = " select id,  name from   xc_danger where  status=1";
            $sql = " select id,  name from   xc_danger where status=1";
            $Data = M('danger'); //实例化Data数据模型
            $list = $Data->query($sql);
            if (!empty($list)) {
                foreach ($list as $k => $v) {
                    //根据风险点ID查询，风险等级为1的
                    //  $list[$k]['type_name'] = M('ObjectType')->where(['id' => $v['type_id']])->getField('name');
                    $sql1="select *  from xc_object where  status=1 and danger_id =".$v['id'];
                    $Data1 = M('object'); //实例化Data数据模型
                    $res2 =  $Data1->query($sql1);
                    $list[$k]['voo'] = $res2;
                }
            }
            $this->assign('list', $list);
            $this->display('test_list');


    }
    public  function  test_wxy()
    {
        $object_id= $_GET['object_id'];
        $where1['object_id']=$object_id;
        $where1['status']='1';
//echo $object_id;
        $list = $this->lists('Object',$where1, 'object_id asc');
     //   dump($list);
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                $list[$k]['danger_name'] = M('Danger')->where(['id' => $v['danger_id']])->getField('name');
                $list[$k]['object_level_name'] = M('ObjectLevel')->where(['id' => $v['object_level_id']])->getField('name');

// 根据危险源ID object_id 查询该危险源本月，累计巡检次数XX次，未巡检多少次
                //execute返回结果数
                $start = date('Y-m-01 00:00:00');
                $end = date('Y-m-d H:i:s');
                $sqlw= "SELECT push_id  FROM `xc_push_record` WHERE object_id =".$v['object_id']." and `create_time` >= unix_timestamp('”.$start.”') AND `create_time` <= unix_timestamp('$end')";
                $model = M( "push_record" );
                // 本月已经巡检次数count1,
                $list[$k]['count1']  =$model ->execute($sqlw);

                //  本月岗位级别级巡检次数count_gs1 ， 未巡检次数等于当天日期减去已巡检次数  date('d') -
                $sqlgw=   "select push_id  from  xc_push_record t  where  object_id =".$v['object_id']." and push_user_id  in (select t.user_id  from  xc_user t  where  t.level_id=5  and t.status=1)";
                $gw1 = M( "push_record" );
                // 本月员工巡检次数yg1，未巡检次数yg2,
                $list[$k]['yg1']  =$gw1 ->execute($sqlgw);
                $list[$k]['yg2']  =   date('d') - $list[$k]['yg1']  ;
                //本月班组级别级巡检次数count_gs1 ， 未巡检次数等于当天日期减去已巡检次数  date('d') -
                $sqlbz=   "select push_id  from  xc_push_record t  where  object_id =".$v['object_id']." and   push_user_id  in (select t.user_id  from  xc_user t  where  t.level_id=4  and t.status=1)";
                $bz1 = M( "push_record" );
                // 本月班组巡检次数yg1，未巡检次数yg2,
                $list[$k]['bz1']  =$bz1 ->execute($sqlbz);
                $list[$k]['bz2']  =   date('d') - $list[$k]['bz1']  ;
                //本月车间级别级巡检次数cj1 ， 未巡检次数 风险点车间级每月巡检4次-
                $sqlcj=   "select push_id  from  xc_push_record t  where  object_id =".$v['object_id']." and   push_user_id  in (select t.user_id  from  xc_user t  where  t.level_id=3  and t.status=1)";
                $cj1 = M( "push_record" );
                // 本月班组巡检次数yg1，未巡检次数yg2,
                $list[$k]['cj1']  =$cj1 ->execute($sqlcj);
                $list[$k]['cj2']  = 4 - $list[$k]['cj1']  ;
                //本月专业级别级巡检次数cj1 ， 未巡检次数 风险点车间级每月巡检4次-
                $sqlzy=   "select push_id  from  xc_push_record t  where  object_id =".$v['object_id']." and   push_user_id  in (select t.user_id  from  xc_user t  where  t.level_id=2  and t.status=1)";
                $zy1 = M( "push_record" );
                // 本月班组巡检次数yg1，未巡检次数yg2,
                $list[$k]['zy1']  =$zy1 ->execute($sqlzy);
                $list[$k]['zy2']  = 3 - $list[$k]['zy1']  ;
                //  本月公司级别级巡检次数count_gs1 ，公司级应每月巡检1次
                $sqlgs=   "select push_id  from  xc_push_record t  where  object_id =".$v['object_id']." and   push_user_id  in (select t.user_id  from  xc_user t  where  t.level_id=1  and t.status=1)";
                $gs1 = M( "push_record" );
                // 本月公司巡检次数count1,
                $list[$k]['gs1']  =$gs1 ->execute($sqlgs);
                $list[$k]['gs2']  = 1 - $list[$k]['gs1']  ;

                $list[$k]['count2']  =   $list[$k]['gs2']+ $list[$k]['zy2'] +    $list[$k]['cj2']+ $list[$k]['yg2']+  $list[$k]['bz2'];

            }
        }




        $this->assign('list', $list);
   //     $Page     = new \Think\Page($count, 20);
     //   $show      = $Page->show();
   //     $this->assign('page', $show);

        $this->display('test_wxy');


    }
}